package com.elitel;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class RhinoExample {
    public static void main(String[] args) {
        // 创建JavaScript引擎
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");

        try {
            // 执行简单的JavaScript代码
            engine.eval("var msg = 'Hello from JavaScript!';");
            engine.eval("print(msg);");

            // 调用JavaScript函数
            engine.eval("function add(a, b) { return a + b; }");
            Object result = engine.eval("add(3, 4);");
            System.out.println("Result: " + result);

        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}